<?php

namespace Biz\model\SuperSubsidy;

use Doraemon\pockets\datebase\BaseDbModel;

/**
 * 订单发货记录
 */
class YouZanDeliverRecordModel extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_brand_activity';

    const _DATABASE_ = 'brand_activity';

    const _TABLE_NAME = 'youzan_deliver_record';

    const TYPE_SEND = 1;
    const TYPE_CHANGE = 2;

    const TYPE_MAP = [
        self::TYPE_SEND => '发货',
        self::TYPE_CHANGE => '修改物流',
    ];

    const STATUS_CREATE = 0;//已提交
    const STATUS_DONE = 1;//已完成
    const STATUS_DOING = 2;//处理中

    const STATUS_MAP = [
        self::STATUS_CREATE => '已提交',
        self::STATUS_DOING => '处理中',
        self::STATUS_DONE => '已完成',
    ];

    public static function genRecordNo($brandUserId, $type = self::TYPE_SEND)
    {
        $reserved = date('YmdHis');
        $rand = sprintf("%04d", mt_rand(0, 999));
        $uid_tail = substr($brandUserId, -4);
        $uid_tail = sprintf("%04d", $uid_tail);
        $recordNo = $reserved . $rand . $type . $uid_tail;
        $info = self::getByWhere(['record_no' => $recordNo]);
        if (!empty($info)) {
            return self::genRecordNo($brandUserId, $type);
        }
        return $recordNo;
    }


    public static function getByWhere($where, $field = '*')
    {
        return self::get($field, $where);
    }

    public static function selectByWhere($where, $select = '*')
    {
        $list = self::select($select, $where);

        return $list;
    }

    public static function selectCountWhere($where)
    {
        $count = self::count('id', $where);

        return $count;
    }


    public static function insertData($data)
    {
        if (!isset($data['created_at'])) {
            $data['created_at'] = date('Y-m-d H:i:s');
        }
        if (!isset($data['update_at'])) {
            $data['update_at'] = date('Y-m-d H:i:s');
        }
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return self::id();
    }


    public static function updateData($data, $where)
    {
        if (!isset($data['update_at'])) {
            $data['update_at'] = date('Y-m-d H:i:s');
        }
        $ret = self::update($data, $where);
        if ($ret->errorCode() !== '00000') {
            return false;
        }

        return true;
    }
}